<?php
class Paginate 
{
	public $pageno=0;
	public $stages=3;
	
	public function getnewpager($page=1,$totalrecords=1)
	{
		$this->pageno=$page;
		
		if ($page == 0){$page = 1;}
		$prev = $page - 1;	
		$Next = $page + 1;							
		$lastpage = ceil($totalrecords/PAGELIMIT);		
		$LastPagem1 = $lastpage - 1;
		
		if($prev<=0)
		{
			$prev=1;
		}
		
		/* 
			Now we apply our rules and draw the pagination object. 
			We're actually saving the code to a variable in case we want to draw it more than once.
		*/
		
		$paginate = '';
		if($lastpage > 1)
		{
			$paginate .= "<div class='pagination'>";
			if ($page > 1){
				
				$paginate.= "<a href='javascript:void(0);' onclick='getdata(1)' class='button'><span><img src='images/arrow-stop-180-small.gif'  height='9' width='12' alt='First' /> First</a>";
				$paginate.= "<a href='javascript:void(0);' onclick='getdata($prev)' class='button'><span><img src='images/arrow-180-small.gif'  height='9' width='12' alt='Previous' /> Prev</a>";
			}else{
				$paginate.= "<a href='javascript:void(0);' class='button' disabled='true'><span><img src='images/arrow-stop-180-small.gif'  height='9' width='12' alt='First' /> First</a>";
				$paginate.= "<a href='javascript:void(0);' class='button' disabled='true'><span><img src='images/arrow-180-small.gif'  height='9' width='12' alt='Previous' /> Prev</a>";
			}
				
			$paginate.="<div class='numbers'><span>Page:</span> ";
			
			// Pages	
			if ($lastpage < 7 + ($this->stages * 2))	// Not enough pages to breaking it up
			{	
				for ($counter = 1; $counter <= $lastpage; $counter++)
				{
					if ($counter == $page){
						$paginate.= "<span class='current'>$counter</span><span>|</span>";
					}else{
						//$paginate.= "<a href='$targetpage?language_id_val=".$_GET['language_id_val']."&page=$counter'>$counter</a>";
						$paginate.= "<a href='javascript:void(0);' onclick='getdata($counter)'>$counter</a><span>|</span>";
					}					
				}
			}
			elseif($lastpage > 5 + ($this->stages * 2))	// Enough pages to hide a few?
			{
				// Beginning only hide later pages
				if($page < 1 + ($this->stages * 2))		
				{
					for ($counter = 1; $counter < 4 + ($this->stages * 2); $counter++)
					{
						if ($counter == $page){
							$paginate.= "<span class='current'>$counter</span><span>|</span>";
						}else{
							//$paginate.= "<a href='$targetpage?language_id_val=".$_GET['language_id_val']."&page=$counter'>$counter</a>";
								$paginate.= "<a href='javascript:void(0);'onclick='getdata($counter)'>$counter</a><span>|</span>";
							}					
							
					}
					$paginate.= "<span>...</span>";
					//$paginate.= "<a href='$targetpage?language_id_val=".$_GET['language_id_val']."&page=$LastPagem1'>$LastPagem1</a>";
					$paginate.= "<a href='javascript:void(0);'onclick='getdata($LastPagem1)'>$LastPagem1</a><span>|</span>";
					//$paginate.= "<a href='$targetpage?language_id_val=".$_GET['language_id_val']."&page=$lastpage'>$lastpage</a>";		
					$paginate.= "<a href='javascript:void(0);'onclick='getdata($lastpage)'>$lastpage</a><span>|</span>";
				}
				// Middle hide some front and some back
				elseif($lastpage - ($this->stages * 2) > $page && $page > ($this->stages * 2))
				{
					//$paginate.= "<a href='$targetpage?language_id_val=".$_GET['language_id_val']."&page=1'>1</a>";
					$paginate.= "<a href='javascript:void(0);'onclick='getdata(1)'>1</a><span>|</span>";
					//$paginate.= "<a href='$targetpage?language_id_val=".$_GET['language_id_val']."&page=2'>2</a>";
					$paginate.= "<a href='javascript:void(0);'onclick='getdata(2)'>2</a><span>|</span>";
					$paginate.= "<span>...</span>";
					for ($counter = $page - $this->stages; $counter <= $page + $this->stages; $counter++)
					{
						if ($counter == $page){
							$paginate.= "<span class='current'>$counter</span><span>|</span>";
						}else{
							//$paginate.= "<a href='$targetpage?language_id_val=".$_GET['language_id_val']."&page=$counter'>$counter</a>";	
							$paginate.= "<a href='javascript:void(0);'onclick='getdata($counter)'>$counter</a><span>|</span>";
							}					
					}
					$paginate.= "<span>...</span>";
					//$paginate.= "<a href='$targetpage?language_id_val=".$_GET['language_id_val']."&page=$LastPagem1'>$LastPagem1</a>";
					$paginate.= "<a href='javascript:void(0);'onclick='getdata($LastPagem1)'>$LastPagem1</a><span>|</span>";
					//$paginate.= "<a href='$targetpage?language_id_val=".$_GET['language_id_val']."&page=$lastpage'>$lastpage</a>";		
					$paginate.= "<a href='javascript:void(0);'onclick='getdata($lastpage)'>$lastpage</a><span>|</span>";
					
				}
				// End only hide early pages
				else
				{
					//$paginate.= "<a href='$targetpage?language_id_val=".$_GET['language_id_val']."&page=1'>1</a>";
					$paginate.= "<a href='javascript:void(0);'onclick='getdata(1)'>1</a><span>|</span>";
					//$paginate.= "<a href='$targetpage?language_id_val=".$_GET['language_id_val']."&page=2'>2</a>";
					$paginate.= "<a href='javascript:void(0);'onclick='getdata(2)'>2</a><span>|</span>";
					$paginate.= "<span>...</span>";
					for ($counter = $lastpage - (2 + ($this->stages * 2)); $counter <= $lastpage; $counter++)
					{
						if ($counter == $page){
							$paginate.= "<span class='current'>$counter</span><span>|</span>";
						}else{
							//$paginate.= "<a href='$targetpage?language_id_val=".$_GET['language_id_val']."&page=$counter'>$counter</a>";
							$paginate.= "<a href='javascript:void(0);'onclick='getdata($counter)'>$counter</a><span>|</span>";
							}					
					}
				}
			}
			$paginate=substr($paginate,0,(strlen($paginate)-14));
			$paginate.="</div>";			
					// Next
			if ($page < $counter - 1){ 
				$paginate.= "<a href='javascript:void(0);' onclick='getdata($Next)' class='button'><span>Next <img src='images/arrow-000-small.gif'  height='9' width='12' alt='Next' /></a>";
				$paginate.= "<a href='javascript:void(0);' onclick='getdata($lastpage)' class='button'><span>Last <img src='images/arrow-stop-000-small.gif'  height='9' width='12' alt='Last' /></a>";
			}else{
				$paginate.= "<a href='javascript:void(0);' class='button' disabled='true'><span>Next <img src='images/arrow-000-small.gif'  height='9' width='12' alt='Next' /></a>";
				$paginate.= "<a href='javascript:void(0);' class='button' disabled='true'><span>Last <img src='images/arrow-stop-000-small.gif'  height='9' width='12' alt='Last' /></a>";
			}
				
			$paginate.= "</div>";		
		
		
		}
		return $paginate;
	}
	 
}

?>